package main

import (
	"fmt"
	"github.com/gorilla/sessions"
	"log"
	"net/http"
	"net/http/httputil"
)

var (
	store = sessions.NewCookieStore([]byte("my-secret"))
	sName = "my-session"
	mName = "my-name"
)

func main() {
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		data, _ := httputil.DumpRequest(req, true)
		//fmt.Println(fmt.Printf("%s", data))
		fmt.Fprintf(res, "%s", data)
		session, _ := store.Get(req, sName)
		fmt.Println(session.Values)
	})

	http.HandleFunc("/auth", AuthHandler)
	http.HandleFunc("/get", GetHandler)

	err := http.ListenAndServe("127.0.0.1:9090", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func AuthHandler(w http.ResponseWriter, req *http.Request) {
	session, _ := store.Get(req, sName)

	session.Values[mName] = "gary"
	session.Save(req, w)
	fmt.Fprintf(w, "login")
}

func GetHandler(w http.ResponseWriter, req *http.Request) {
	session, _ := store.Get(req, sName)
	fmt.Fprintf(w, "%s", session.Values[mName])
}
